package com.ruoyi.sales.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.sales.domain.vo.SalesRecordVo;
import com.ruoyi.sales.domain.bo.SalesRecordBo;
import com.ruoyi.sales.service.ISalesRecordService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 售后服务记录
 *
 * @author ruoyi
 * @date 2025-08-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/sales/record")
public class SalesRecordController extends BaseController {

    private final ISalesRecordService iSalesRecordService;

    /**
     * 查询售后服务记录列表
     */
    @SaCheckPermission("sales:record:list")
    @GetMapping("/list")
    public TableDataInfo<SalesRecordVo> list(SalesRecordBo bo, PageQuery pageQuery) {
        return iSalesRecordService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出售后服务记录列表
     */
    @SaCheckPermission("sales:record:export")
    @Log(title = "售后服务记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SalesRecordBo bo, HttpServletResponse response) {
        List<SalesRecordVo> list = iSalesRecordService.queryList(bo);
        ExcelUtil.exportExcel(list, "售后服务记录", SalesRecordVo.class, response);
    }

    /**
     * 获取售后服务记录详细信息
     *
     * @param saleId 主键
     */
    @SaCheckPermission("sales:record:query")
    @GetMapping("/{saleId}")
    public R<SalesRecordVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long saleId) {
        return R.ok(iSalesRecordService.queryById(saleId));
    }

    /**
     * 新增售后服务记录
     */
    @SaCheckPermission("sales:record:add")
    @Log(title = "售后服务记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SalesRecordBo bo) {
        return toAjax(iSalesRecordService.insertByBo(bo));
    }

    /**
     * 修改售后服务记录
     */
    @SaCheckPermission("sales:record:edit")
    @Log(title = "售后服务记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SalesRecordBo bo) {
        return toAjax(iSalesRecordService.updateByBo(bo));
    }

    /**
     * 删除售后服务记录
     *
     * @param saleIds 主键串
     */
    @SaCheckPermission("sales:record:remove")
    @Log(title = "售后服务记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{saleIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] saleIds) {
        return toAjax(iSalesRecordService.deleteWithValidByIds(Arrays.asList(saleIds), true));
    }
}
